package com.enzoism.controller;

import com.enzoism.constants.Constants;
import com.enzoism.model.User;
import com.enzoism.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController//该注解替换了原来的@Controller注解
@RequestMapping("user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @GetMapping("hello")
    public String hello(){
        return  "hello";
    }
 

    @GetMapping("allUserList")
    public List<User> getAllUserList(){
        List<User> userList = userService.getAllUserList();
        return userList;
    }

    @GetMapping("pageUserList")
    @Cacheable(cacheNames = "pageUserList")
    public Map<String,Object> pageUserList(){
        // 使用分页插件,核心代码就这一行，页数、每页行数
        Page<Object> page = PageHelper.startPage(Constants.pageNum, Constants.pageSize);
        // 结果返回
        Map<String,Object> resultMap = new HashMap<>();
        List<User> userList = userService.getAllUserList();
        resultMap.put("userList", userList);
        resultMap.put("total", page.getTotal());
        return resultMap;
    }
}